package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.bo.HcxUserOpinionsPicsAddBo;
import com.ruoyi.business.bo.HcxUserOpinionsPicsQueryBo;
import com.ruoyi.business.bo.HcxUserOpinionsPicsEditBo;
import com.ruoyi.business.domain.HcxUserOpinionsPics;
import com.ruoyi.business.mapper.HcxUserOpinionsPicsMapper;
import com.ruoyi.business.vo.HcxUserOpinionsPicsVo;
import com.ruoyi.business.service.IHcxUserOpinionsPicsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户反馈意见图片Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Service
public class HcxUserOpinionsPicsServiceImpl extends ServicePlusImpl<HcxUserOpinionsPicsMapper, HcxUserOpinionsPics> implements IHcxUserOpinionsPicsService {

    @Override
    public HcxUserOpinionsPicsVo queryById(Long id){
        return getVoById(id, HcxUserOpinionsPicsVo.class);
    }

    @Override
    public TableDataInfo<HcxUserOpinionsPicsVo> queryPageList(HcxUserOpinionsPicsQueryBo bo) {
        PagePlus<HcxUserOpinionsPics, HcxUserOpinionsPicsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxUserOpinionsPicsVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HcxUserOpinionsPicsVo> queryList(HcxUserOpinionsPicsQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxUserOpinionsPicsVo.class);
    }

    private LambdaQueryWrapper<HcxUserOpinionsPics> buildQueryWrapper(HcxUserOpinionsPicsQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxUserOpinionsPics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HcxUserOpinionsPics::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), HcxUserOpinionsPics::getUserName, bo.getUserName());
        lqw.like(StrUtil.isNotBlank(bo.getRealName()), HcxUserOpinionsPics::getRealName, bo.getRealName());
        lqw.eq(bo.getOpinionId() != null, HcxUserOpinionsPics::getOpinionId, bo.getOpinionId());
        lqw.eq(StrUtil.isNotBlank(bo.getPicUrl()), HcxUserOpinionsPics::getPicUrl, bo.getPicUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxUserOpinionsPics::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HcxUserOpinionsPicsAddBo bo) {
        HcxUserOpinionsPics add = BeanUtil.toBean(bo, HcxUserOpinionsPics.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxUserOpinionsPicsEditBo bo) {
        HcxUserOpinionsPics update = BeanUtil.toBean(bo, HcxUserOpinionsPics.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxUserOpinionsPics entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxUserOpinionsPics(List<HcxUserOpinionsPicsVo> hcxUserOpinionsPicsList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxUserOpinionsPicsList), "导入数据不能为空");
        int successNum = 0;
        for (HcxUserOpinionsPicsVo hcxUserOpinionsPics : hcxUserOpinionsPicsList) {
            HcxUserOpinionsPics add = BeanUtil.toBean(hcxUserOpinionsPics, HcxUserOpinionsPics.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxUserOpinionsPicsList.size(), successNum);
        if (successNum != hcxUserOpinionsPicsList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxUserOpinionsPicsList.size() - successNum);
        }
        return msg;
    }
}
